package cn.servlet;

import cn.entity.Users;
import cn.service.UsersService;
import cn.service.impl.UsersServiceImpl;
import cn.util.Const;
import cn.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//后台登陆
@WebServlet("/users")
public class UsersServlet extends HttpServlet {
	UsersService usersService=new UsersServiceImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String action=request.getParameter("action");

		switch(action){
		case "login":
			login(request,response);
			break;
		case "reg":
			reg(request,response);
			break;
		case "logout":
			logout(request,response);
			break;
		}
	}
	
	protected void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		//获取用户传过来的username
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String md5pass=MD5Util.getMD5(password);
		//调用业务类alt+/
	
		Users users=usersService.login(username, md5pass,1);
		HttpSession session=request.getSession();
		//判断用户名密码是否正确admin   123
		if(users!=null){
			//将用户放入session 
			session.setAttribute("users", users);
			session.setAttribute("role", 1);
			//成功,重定向,2次请求，重新发起的,新闻列表
			response.sendRedirect(Const.ROOT+"index");
			//请求转发,1次请求，服务器内部请求
			//request.getRequestDispatcher("success.jsp").forward(request, response);
		}else{
			out.print("<script>alert('用户名或者密码错误');location.href='"+Const.ROOT+"login.jsp';</script>");
		}
		out.close();
	}
	
	protected void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		session.invalidate();
		response.sendRedirect(Const.ROOT+"login.jsp");
	}
	
	protected void reg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String name=request.getParameter("name");
		String phone=request.getParameter("phone");
		String pic=request.getParameter("pic");
		String address=request.getParameter("address");
		Users users=new Users();
		users.setUsername(username);
		users.setPassword(MD5Util.getMD5(password));
		users.setName(name);
		users.setPic(pic);
		users.setPhone(phone);
		users.setAddress(address);
		users.setRole(1);
		users.setIsdel(0);
		int n=usersService.insert(users);
		if(n>0){
			response.sendRedirect(Const.ROOT+"login.jsp");
		}else{
			out.print("<script>alert('注册失败');location.href='"+Const.ROOT+"login.jsp';</script>");
		}
		out.close();
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
